package com.huacheng.huiservers.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.huacheng.huiservers.R;
import com.huacheng.huiservers.fragment.bean.ShopIndexBean;
import com.huacheng.huiservers.shop.ShopListActivity;
import com.huacheng.huiservers.utils.MyCookieStore;
import com.huacheng.huiservers.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类：
 * 时间：2018/5/29 13:01
 * 功能描述:Huiservers
 */
public class ShopIndexLodingRecycleCateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<ShopIndexBean> mList = new ArrayList<ShopIndexBean>();

    private Context mContext;

    // 普通布局
    private final int TYPE_ITEM = 1;
    // 脚布局
    private final int TYPE_FOOTER = 2;
    // 当前加载状态，默认为加载完成
    private int loadState = 2;
    // 正在加载
    public final int LOADING = 1;
    // 加载完成
    public final int LOADING_COMPLETE = 2;
    // 加载到底
    public final int LOADING_END = 3;

    TextView mTvShopNum;

    public ShopIndexLodingRecycleCateAdapter(Context context, TextView tvShopNum, List<ShopIndexBean> mList) {
        this.mList = mList;
        this.mContext = context;
        this.mTvShopNum = tvShopNum;

    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        return TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 通过判断显示类型，来创建不同的View
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.shop_myshop_bottom_banner_item, parent, false);
            return new RecyclerViewHolder(view);

        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_refresh_footer, parent, false);
            return new FootViewHolder(view);
        }
        return null;
    }

    ShopIndexLodingRecycleAdapter listAdapter;
    GridLayoutManager manager;

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof RecyclerViewHolder) {
            RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;

            if (!StringUtils.isEmpty(mList.get(position).getCate_img())) {
                recyclerViewHolder.cardview_shop_banner.setVisibility(View.VISIBLE);

                Glide.with(mContext)
                        .load(MyCookieStore.URL + mList.get(position).getCate_img())
                        .skipMemoryCache(false)
                       /* .error(R.drawable.icon_girdview)
                        .placeholder(R.drawable.icon_girdview)*/
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .into(recyclerViewHolder.iv_shop_list_banner);

                recyclerViewHolder.iv_shop_list_banner.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String cateID = mList.get(position).getId();
                        if (!StringUtils.isEmpty(cateID)) {
                            Intent intent = new Intent(mContext, ShopListActivity.class);
                            intent.putExtra("cateID", cateID);
                            mContext.startActivity(intent);
                        }
                    }
                });
            } else {
                recyclerViewHolder.cardview_shop_banner.setVisibility(View.GONE);
            }


            if (mList.get(position).getList() != null) {
                manager = new GridLayoutManager(mContext, 2);
                recyclerViewHolder.mRecyclerView.setLayoutManager(manager);
                listAdapter = new ShopIndexLodingRecycleAdapter(mContext, mTvShopNum, mList.get(position).getList());
                recyclerViewHolder.mRecyclerView.setAdapter(listAdapter);
            }


        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            switch (loadState) {
                /*case LOADING: // 正在加载
                    footViewHolder.pbLoading.setVisibility(View.VISIBLE);
                    footViewHolder.tvLoading.setVisibility(View.VISIBLE);
                    footViewHolder.llEnd.setVisibility(View.GONE);
                    break;

                case LOADING_COMPLETE: // 加载完成
                    footViewHolder.pbLoading.setVisibility(View.INVISIBLE);
                    footViewHolder.tvLoading.setVisibility(View.INVISIBLE);
                    footViewHolder.llEnd.setVisibility(View.GONE);
                    break;

                case LOADING_END: // 加载到底
                    footViewHolder.pbLoading.setVisibility(View.GONE);
                    footViewHolder.tvLoading.setVisibility(View.GONE);
                    footViewHolder.llEnd.setVisibility(View.VISIBLE);
                    break;*/

                default:
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    // 如果当前是footer的位置，那么该item占据2个单元格，正常情况下占据1个单元格
                    return getItemViewType(position) == TYPE_FOOTER ? gridManager.getSpanCount() : 1;

                }
            });
        }
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder {
        CardView cardview_shop_banner;
        ImageView iv_shop_list_banner;
        RecyclerView mRecyclerView;

        RecyclerViewHolder(View v) {
            super(v);
            cardview_shop_banner = (CardView) v.findViewById(R.id.cardview_shop_banner);
            iv_shop_list_banner = (ImageView) v.findViewById(R.id.iv_shop_list_banner);
            mRecyclerView = (RecyclerView) v.findViewById(R.id.mRecyclerView);
        }

    }

    private class FootViewHolder extends RecyclerView.ViewHolder {

        ProgressBar pbLoading;
        TextView tvLoading;
        LinearLayout llEnd;

        FootViewHolder(View itemView) {
            super(itemView);
            pbLoading = (ProgressBar) itemView.findViewById(R.id.pb_loading);
            tvLoading = (TextView) itemView.findViewById(R.id.tv_loading);
            llEnd = (LinearLayout) itemView.findViewById(R.id.ll_end);
        }
    }

    /**
     * 设置上拉加载状态
     *
     * @param loadState 0.正在加载 1.加载完成 2.加载到底
     */
    public void setLoadState(int loadState) {
        this.loadState = loadState;
        notifyDataSetChanged();
    }

}


